# -*- coding: utf-8 -*-

import pygame.draw
import pygame.font

import default

def popup(lines, color, bgcolor, f_size = 15):
    """Create a popup text surface to show on the screen.
    lines are the lines to write (this method is to stupid
    to handle linebreaks"""

    font = pygame.font.Font(default.font, f_size)
    
    surfaces = []
    longest = 0
    
    for line in lines:
        surf = font.render(line, True, color)
        surfaces.append(surf)
        if longest < surf.get_width():
            longest = surf.get_width()

    height = (len(lines) - 1) * font.get_height() + len(lines) * font.get_linesize()

    textbox = pygame.Surface((longest + 20, height + 20))
    textbox_rect = textbox.get_rect()
    
    if bgcolor is not None:
        textbox.fill(bgcolor)
    else:
        if color != (229, 31, 0):
            dead_color = (229, 31, 0)
        else:
            dead_color = (229, 32, 0)
        textbox.fill(dead_color)
        textbox.set_colorkey(dead_color)
    
    draw_y = 10

    for surface in surfaces:
        surface_rect = surface.get_rect()
        pos = (textbox_rect.centerx - surface_rect.w/2, draw_y)
        textbox.blit(surface, pos)
        draw_y += font.get_height() + font.get_linesize()

    return textbox
